<?php

// +----------------------------------------------------------------------
// |订单服务层
// +----------------------------------------------------------------------

namespace app\common\system;

use app\admin\Model\CountryModel;
use think\facade\Validate;
use think\facade\Db;
use app\common\Model\OrderModel;
use app\common\Model\StroageModel;

class OrderService
{
 
    /**
     * 获取订单列表 的列表
     * @param array $param 前台传过来的参数  
     *   
     */
      public static function list($param,$logistice_channel,$shops){     
        $params=$param['params'];
        $where = [];
        if($params['status']>1 ){
            $where[] = ['status', '=', $params['status']];
        }
         // 查询前 临时禁用 ONLY_FULL_GROUP_BY（开发环境可用）
        Db::execute("SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");
        $field = 'id,orderid,shop_id,status,ordertime,amount,code,logistics_name,trackingno,shipprice,count,l_id,storage_id,notes,weighttime,lastestShiptime,country';     
        $order_list =(new OrderModel())->field($field)->where($where)->order('id asc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();         
       // 查询后恢复
        Db::execute("SET SESSION sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'))");  
        if($order_list['data']){
          $id=array_column($order_list['data'],'id'); 
          $status=(new OrderModel())->getAllStatus();
          //获取国家
          $config_list=(new CountryModel())->getcountryAllList();

          //仓库的信息
          $Model=new StroageModel();
          $storagelist =  $Model->getActivelist();  
          $storagelist =array_column($storagelist,null,'id');
          $logistice_channel =array_column($logistice_channel,null,'id');//物流渠道
          $shops =array_column($shops,null,'id');//店铺信息
        
          if($id){
                $deliver_item = Db::name('deliver_items')->field($field)->whereIn('d_id',$id)->order('id asc')->column('localsku,d_id,count,amount,smallimage,unitprice'); //子体
              
                if($deliver_item)  {
                  
                    foreach ($order_list['data'] as $key => $value) {
                         foreach ($deliver_item as $item) {
                              if ($item['d_id'] == $value['id']) {
                                 $order_list['data'][$key]['children'][]  = $item;
                              }
                          }                    
                        $order_list['data'][$key]['status'] = $status[$value['status']];        
                        if($value['weighttime']>0){
                          $order_list['data'][$key]['weighttime'] = date('Y-m-d H:i:s',$value['weighttime']);    
                        }else{
                             $order_list['data'][$key]['weighttime'] = '-';    
                        }  
                        $order_list['data'][$key]['countrysCN'] = $config_list[$value['country']]['name'];   
                        $order_list['data'][$key]['storage_name'] = $storagelist[$value['storage_id']]['name'];            
                        $order_list['data'][$key]['logistice_name'] = $logistice_channel[$value['l_id']]['name'];   //物流渠道
                        $order_list['data'][$key]['shops_name'] = $shops[$value['shop_id']]['name'];     //店铺信息       
                    }
                }
            }
        } 
        $data['list']= $order_list['data'];
        $data['total']=$order_list['total'];     
        return $data;
     }


    /**
     * 获取订单列表 的列表
     * @param array $param 前台传过来的参数  
     *   
     */
    public static function dateil($param){
            $params=$param['params'];
            $orderid=$params['orderid'];
            $where[] = ['orderid', '=', $orderid];
            $field = 'id,orderid,status,logistics_name,trackingno,count,l_id,storage_id,country,province,city,detailaddress,postcode,contactperson,phone';     
            $order_list =(new OrderModel())->field($field)->where($where)->order('id asc')->select()->toArray();             
            if($order_list){
               $id=array_column($order_list,'id'); 
                $deliver_item = Db::name('deliver_items')->field($field)->whereIn('d_id',$id)->order('id asc')->column('localsku,sellersku,d_id,count,amount,smallimage,unitprice,code'); //子体
                if($deliver_item)  {
                    $status=(new OrderModel())->getAllStatus();
                  //仓库的信息
                    $Model=new StroageModel();
                    $storagelist =  $Model->getActivelist();  
                    $storagelist =array_column($storagelist,null,'id');
                    foreach ($order_list as $key => $value) {
                        foreach ($deliver_item as $item) {
                            if ($item['d_id'] == $value['id']) {
                                $order_list[$key]['children'][]  = $item;
                            }
                        }  
                         $order_list[$key]['storage_name'] = $storagelist[$value['storage_id']]['name'];     
                         $order_list[$key]['status'] = $status[$value['status']];              
                   }
                }
            }
            //查询订单处理过程
              $map[]=['orderid','=', $orderid];
              $log =Db::name('deliver_log')->where($map)->order('id asc')->select()->toArray();   
              if($log){
                foreach ($log as $key => $value) {                    
                   
                     $log[$key]['content'] ='发货单：'.$value['d_id'].'-'.$value['content'];
                }
              }
              $data['list']= $order_list;
              $data['logs']= $log;
              return $data;            

    }


    /**
     * 修改订单的备注
     * @param array $param 前台传过来的参数  
     *   
     */
     public static function confirmnotes($param){
        $params=$param['params'];
        $id=$params['id'];   
        $notes=$params['notes'];
        $notes_info = (new OrderModel())->where('id', $id)->update(['notes' => $notes]);  
        if($notes_info){
           return true;
        }else{
              error('修改备注失败');
        }  
     }

     /**
     * 首页看板数据
     * @param array $param 前台传过来的参数  
     *   
     */
     public static function lists($param){
          $params=$param['params'];            
         // 查询前 临时禁用 ONLY_FULL_GROUP_BY（开发环境可用）
        Db::execute("SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");
        $field = 'id,orderid,shop_id,status,ordertime,code,count,weighttime,lastestShiptime';     
        $order_list =(new OrderModel())->field($field)->order('id asc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray();         
       // 查询后恢复
        Db::execute("SET SESSION sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'))");  
         if($order_list['data']){         
          $status=(new OrderModel())->getAllStatus();
          foreach ($order_list['data'] as $key => $value) {                 
                $order_list['data'][$key]['status'] =  $status[$value['status']];       
                if($value['weighttime']>0){
                  $order_list['data'][$key]['weighttime'] = date('Y-m-d H:i:s',$value['weighttime']);    
                }else{
                      $order_list['data'][$key]['weighttime'] = '-';    
                }         
            }
         }
        $data['list']= $order_list['data'];
        $data['total']=$order_list['total'];  
        return $data;     
     }
}